<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/8
 * Time: 17:55
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\HotKeywordModel;

class KeywordService extends BaseService
{
    /**
     * 热门关键词列表
     * @return array
     */
    public function keywordList()
    {
        $list   =   HotKeywordModel::query()->select(['id', 'keyword', 'sorting'])->orderBy('sorting', 'desc')->get();
        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, $list->toArray()];
        }
    }

    /**
     * 热门关键词添加
     * @param array $params  添加参数
     * @return array
     */
    public function keywordAdd(array $params)
    {
        $insert['keyword']  =   $params['keyword'];
        $insert['sorting']  =   $params['sorting'];

        $insert_result  =   HotKeywordModel::create($insert);
        if ($insert_result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * 热门关键词删除
     * @param int $key_id  关键词ID
     * @return array
     */
    public function keywordDelete(int $key_id)
    {
        $delete_result  =   HotKeywordModel::where(['id' => $key_id])->delete();
        if ($delete_result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }

    /**
     * 热门关键词修改
     * @param array $params  修改参数
     * @param int $key_id   关键词ID
     * @return array
     */
    public function keywordEdit(array $params, int $key_id)
    {
        $update['keyword']  =   $params['keyword'];
        $update['sorting']  =   $params['sorting'] ?? 0;

        $update_result  =   HotKeywordModel::where(['id' => $key_id])->update($update);

        if ($update_result) {
            return [true, '修改成功'];
        } else {
            return [false, '修改失败'];
        }
    }
}
